package com.example.arithmeticleetcode.leetcode.october;

/**
 * @program: arithmetic-leetcode
 * @description:
 * @author: FangZhen
 * @create: 2020-10-19 09:13
 **/
public class Demo08 {

    public static void main(String[] args) {
        System.out.println(maxLengthBetweenEqualCharacters("aa"));
        System.out.println(maxLengthBetweenEqualCharacters("abca"));
        System.out.println(maxLengthBetweenEqualCharacters("cbzxy"));
        System.out.println(maxLengthBetweenEqualCharacters("cabbac"));
        System.out.println(maxLengthBetweenEqualCharacters("mgntdygtxrvxjnwksqhxuxtrv"));
    }

    public static int maxLengthBetweenEqualCharacters(String s) {
        int max = -1;
        for (int i = 0, len = s.length(); i < len; i++) {
            int headIndex = s.charAt(i);
           for (int j = len - 1; j > i; j--) {
               if (headIndex == s.charAt(j)) {
                   int betweenIndex = j - i - 1;
                   if( betweenIndex > max) {
                       max = betweenIndex;
                   }
               }
           }
        }
        return max;
    }
}
